﻿$(document).keyup( function (e) {
    // New task by Ctr + Space
    if ((window.event.ctrlKey == true) && (window.event.keyCode == 32)) {
        GetTaskForm();
    }
    if (e.keyCode == 27) {
        // Hide task form
        e.preventDefault();
        $('#mask, .window').hide();
        $('.add-task-btn').removeClass('pressed');
        clearFormContainer();
    }
});


$('#task-form').live('keyup', function (event, formElem) {
    if ((event.ctrlKey) && ((event.keyCode == 0xA) || (event.keyCode == 0xD))) {
        if ($(this).attr('action') == '/Task/AddTask') {
            AddTask();
        } else {
            SaveTask();
        }
    }
});


function StartApp() {
    var element = $('.main-nav-items').children().next().next().next();
    element.addClass('selected');
    element.children().next().addClass('task-count-selected');
    GetScrollBar();
}

$(window).resize(function () {
    GetScrollBar();
});


function GetScrollBar() {
    $('#main-content').css('height', $(window).height() - 45);
    $('.content').css('height', $(window).height() - 45);    
    jQuery('#main-content.nano').nanoScroller();
}


////Clear form
function clearForm() {
    $('#task-add-form').trigger('reset');
}

function clearFormContainer() {
    $('#list-form').empty();
    $('#task-form-conteiner').empty();
    $('#edit-task-form').empty();
}

function DeleteTask(id) {
    $("#task-id-" + id).remove();
}

function UpdateTaskCount() {
    $.ajax({
        type: 'POST',
        url: 'TaskStatistics/InboxCount',
        success: function (data) {
            $('.inbox-count').html(data);
        }
    });
    $.ajax({
        type: 'POST',
        url: 'TaskStatistics/UpcomingCount',
        success: function (data) {
            $('.upcoming-count').html(data);
        }
    });
    $.ajax({
        type: 'POST',
        url: 'TaskStatistics/AllTasksCount',
        success: function (data) {
            $('.all-count').html(data);
        }
    });
    $.ajax({
        type: 'POST',
        url: 'TaskStatistics/FlaggedCount',
        success: function (data) {
            $('.flagged-count').html(data);
        }
    });
}

function formShow(id) {
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set height and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

    //transition effect     
    $('#mask').fadeTo("fast", 0.0);

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    //$(id).css('top', winH / 2 - $(id).height() / 2);
    $(id).css('top', '150px');
    $(id).css('left', winW / 2 - $(id).width() / 2);

    //transition effect
    $(id).fadeIn(100);
}

$(function () {
    $('#DateDue').live('click', function () {
        $(this).datepicker({ showOn: 'focus', dateFormat: 'dd.mm.yy', firstDay: 1 }).focus();
    });
});

$(document).ready(function () {
    StartApp();

    $(document.body).click(function (ev) {
        $('ul.shared-items').hide();
        $('#login-menu').hide();
    });

    $("a.new-task").click(function () {
        $("#add-task-cont").show();
        $('body').append('<div id="fade"></div>');
        $('#fade').show();
    });

    $('#fade').live('click', function () {
        $('#add-task-cont').hide();
        $('#fade').hide();
    });

    $('.task-checkbox').live("click", function () {
        $(this).parent().parent().toggleClass('task-complete');
        $(this).children().children().toggleClass('check-box-uncheck');
    });


    //select all the a tag with name equal to modal
    $('a[name=add-task]').click(function (e) {
        $(this).toggleClass('pressed');
        var id = $(this).attr('href');
        formShow(id);
    });

    // Edit task----------------------------------------------------------
    $('a[name=edit-task]').click(function (e) {
        e.preventDefault();
        var id = $(this).attr('href');
        formShow(id);
    });

    // New list----------------------------------------------------------
    $('a[name=new-list]').click(function (e) {
        e.preventDefault();
        var id = $(this).attr('href');
        formShow(id);
    });

    $('.window .cancel-edit').live('click', (function (e) {
        //Cancel the link behavior

        e.preventDefault();
        $('#mask, .window').hide();
        clearFormContainer()

    }));

    //if close button is clicked
    $('.window .cancel').live('click', function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
        $('.add-task-btn').removeClass('pressed');
        clearFormContainer();
        
        var sideBarPressed = $('.side-bar-pressed');
        for(i = 0; i < sideBarPressed.size(); i++){
            sideBarPressed.eq(i).removeClass('side-bar-pressed');
        }
    });

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
        $('.add-task-btn').removeClass('pressed');
        $('.settings-btn').removeClass('pressed');

        clearFormContainer();
        var sideBarPressed = $('.side-bar-pressed');
        for(i = 0; i < sideBarPressed.size(); i++){
            sideBarPressed.eq(i).removeClass('side-bar-pressed');
        }
    });

    //if mask is clicked
    $('body').click(function () {
        $('#list-settings-conteiner').empty();
        $('#list-settings-conteiner').hide();
    });

    if ($('#ProjectId').val() == "0") $('#ProjectId').addClass("empty");

    $("#ProjectId").live('change', (function () {
        if ($(this).val() == "0") $(this).addClass("empty");
        else $(this).removeClass("empty")
    }));

});

$('.task-checkbox').live('click', function () {
    $(this).parent().parent().parent().toggleClass('complete');
});

$('.task-checkbox-completed').live('click', function () {
    $(this).parent().parent().parent().toggleClass('complete');
});

$('.side-bar-action').live('click', function () {
    $('.project-list-ico-activ').removeClass('project-list-ico-activ').addClass('project-list-ico');

    $('.selected').toggleClass('selected');
    $('.task-count-selected').toggleClass('task-count-selected');

    $(this).parent().toggleClass('selected');
    $(this).next().addClass('task-count-selected');
});

$('.project-list-action').live('click', function () {
    $('.project-list-ico-activ').removeClass('project-list-ico-activ').addClass('project-list-ico');

    $(this).prev().toggleClass('project-list-ico');
    $(this).prev().toggleClass('project-list-ico-activ');
    $('.selected').toggleClass('selected');
    $('.task-count-selected').toggleClass('task-count-selected');

    $(this).parent().toggleClass('selected');
    $(this).next().addClass('task-count-selected');
});

$('.task-tab').live('click', function () {
    $(this).next().slideToggle(100);
});

//Project---------------------------------------------------------------------------------
$('#project-form').live('keyup', function (event, formElem) {
    if ((event.ctrlKey) && ((event.keyCode == 0xA) || (event.keyCode == 0xD))) {
        if ($(this).attr('action') == '/Project/AddProject') {
            AddProject($('#project-form'));
        } else {
            var listid = $('input#Id').attr('value');
            SaveProject(listid, $('#project-form'));
        }
    }
});


$('.project-create-done').live('click', function () {
    var form = $(this).parent().next();
    AddProject(form);
});


$('.list-action-edit').live('click', function () {
    var listid = $(this).parent().attr('id').split('-')[2];
    $.ajax({
        type: 'GET',
        dataType: 'HTML',
        url: 'Project/EditProject',
        data: { listid: listid },
        success: function (data) {
            $('#list-form').append(data);
            formShow($('#list-form'));
        }
    });
})

$('.project-edit-done').live('click', function () {
    var listid = $(this).parent().next().children().attr('value');
    var form = $(this).parent().next();
    SaveProject(listid, form);
})


$('.list-settings-ico').live('click', function () {
    var listid = $(this).attr('id').split('-')[2];
    var position = $(this).position();

    $.ajax({
        type: 'GET',
        dataType: 'HTML',
        url: 'Project/GetSettings',
        data: { listid: listid },
        success: function (data) {
            $('#list-settings-conteiner').append(data);
            $('#list-settings-conteiner').css('top', position.top + 52);
            $('#list-settings-conteiner').css('left', position.left + 5);
            $('#list-settings-conteiner').show();
        }
    });
});

$('.list-action-delete').live('click', function () {
    var listid = $(this).parent().attr('id').split('-')[2];
    $.confirm({
        'title': 'Delete List',
        'message': '<h3>Are you sure?</h3> <p>You will not be able to retrieve the list.</p>',
        'btnNo': { 'Cancel': { 'action':
                function () {
                } 
        }
        },
        'btnYes': { 'Delete': { 'action':
                function () {
                    $.ajax({
                        type: 'GET',
                        dataType: 'HTML',
                        url: 'Project/DeleteList',
                        data: { listid: listid },
                        success: function () {
                            $('.project-' + listid).remove();
                        }
                    });

                } 
        }
        }
    });
})

//Settings---------------------------------------------------------------
$('.settings-btn').live('click', function(){
    formShow(null);
    $('.settings-btn').addClass('pressed');
});

//Task-------------------------------------------------------------------
$('.add-task-btn').live('click', function () {
    GetTaskForm();
});

$('.task-create-done').live('click', function () {
    AddTask();
});

$('.edit-task-link').live('click', function (e) {
    var taskid = $(this).attr('id').split('-')[2];
    var id = $(this).attr('href');
    $.ajax({
        type: 'GET',
        dataType: 'HTML',
        url: 'Task/EditTask',
        data: { id: taskid },
        success: function (data) {
            $('#edit-task-form').append(data);
            formShow(id);
        }
    });
});

$('.task-save').live('click', function () {
    var taskid = $(this).parent().parent().find('#Id').val();
    SaveTask();

    if($('#task-details:visible').length == 1){
        var currentVisibleTaskId = $('#task-details-content').children().next().val();        
        if(taskid == currentVisibleTaskId){
            GetTaskDetails(taskid);
        }
    }
});

$('.flagged-task').live('click', function () {

    var taskid = $(this).parent().parent().parent().attr('id').split('-')[2];
    $.ajax({
        type: 'POST',
        dataType: 'HTML',
        url: 'Task/FlagTask',
        data: { taskid: taskid },
        success: function (data) {
            if ($('.flaggedlink').parent().attr('class').split(' ').join('') == 'selected') {
                $("#task-id-" + taskid).remove();
            } else {
                $('#task-id-' + taskid).replaceWith(data);
            }
            UpdateTaskCount();
        }
    });
});

$('.delete-task').live('click', function () {
    var taskid = $(this).parent().parent().parent().attr('id').split('-')[2];
    $.confirm({
        'title': 'Delete Task',
        'message': '<h3>Are you sure?</h3> <p>You will not be able to retrieve the task.</p>',
        'btnNo': { 'Cancel': { 'action':
                function () {
                }
        }
        },
        'btnYes': { 'Delete': { 'action':
                function () {
                    $.ajax({
                        type: 'POST',
                        url: 'Task/DeleteTask',
                        data: { id: taskid },
                        success: function () {
                            $("#task-id-" + taskid).remove();
                            UpdateTaskCount();
                            RemoveEmptyTab();
                        }
                    });
                }
        }
        }
    });
});

function RemoveEmptyTab() {
    var taskTabs = $('.task-tab');
    for (i = 0; i < taskTabs.size(); i++) {
        if (!taskTabs.eq(i).next().html().trim()) {            
            taskTabs.eq(i).hide();
        }
    }
}

/* Task details */
$('.task-details-link').live('click', function(){
    var taskid = $(this).parent().parent().attr('id').split('-')[2];
    GetTaskDetails(taskid);
    $('#task-details').show();
    $('#task-details').css('height', '100%');
    $('#main-content').css('width', '68%');
    $('#main-content').addClass('main-content-radius');
    $('.task-tab:first').addClass('main-content-radius');


});

$('.hide-task-details').live('click', function(){
    $('#task-details').hide();
    $('#main-content').css('width', '100%');    
    $('#main-content').removeClass('main-content-radius');
});

/* Task selecting */
/*
$('.task').live('click', function(){
    $(this).toggleClass('task-selected');
});
*/

    $(".inbox").live('click', function () {
        $.ajax({
            type: 'POST',
            url: 'Task/InboxView',
            success: function (data) {
                $('.content').html(data);
                GetScrollBar();
            }
        });
        return false;
    });

    $(".all").live('click', function () {
        $.ajax({
            type: 'POST',
            url: 'Task/AllTask',
            success: function (data) {
                $('.content').html(data);
                GetScrollBar();
            }
        });
        return false;
    });

    $(".upcominglink").live('click', function () {
        $.ajax({
            type: 'POST',
            url: 'Task/UpcomingList',
            success: function (data) {
                $('.content').html(data);
                GetScrollBar();
            }
        });
        return false;
    });

    $(".flaggedlink").live('click', function () {
        $.ajax({
            type: 'POST',
            url: 'Task/FlaggedTask',
            success: function (data) {
                $('.content').html(data);
                GetScrollBar();
            }
        });
        return false;
    });